import { mapState } from 'vuex'
export default {
  props: {
    userId: {
      type: String,
      default: ''
    },
    userNickname: {
      type: String,
      default: ''
    },
    userAvatar: {
      type: String,
      default: ''
    }
  },

  data () {
    return {
      firstLoading: true,
      scrollLoadEvent: null,
      pagination: {
        list: [],
        pageNo: 1,
        pageSize: 20,
        total: 0
      }
    }
  },

  methods: {
    /**
     * 查询关于用户动态、文章、友圈、收藏、关注等列表数据信息
     * @param { String } url axios请求的url接口地址
     * @param { Boolean } isScroll 是否在滚动到底部的时候触发的请求
     */
    async findUserActionList (url, isScroll) {
      if (isScroll) {
        this.pagination.pageNo++
        this.scrollLoadEvent.loadState = false
      }
      try {
        const { pageNo, pageSize } = this.pagination
        const searchParams = {
          pageNo,
          pageSize,
          userId: this.userId
        }
        const { data } = await this.$axios.get(url, {
          params: searchParams
        })
        const list = data.list.map((o) => {
          const limitContent = (o.pointInfo?.content || o.content)?.substring(0, 118).split('\r\n').slice(0, 4).join('\r\n')
          return {
            ...o,
            limitContent: limitContent + ' ...',
            hasLimit: limitContent !== (o.content || o.pointInfo?.content),
            toggle: true
          }
        })
        this.pagination.list.push(...list)
        if (isScroll) {
          if (pageNo >= Math.ceil(data.total / pageSize)) {
            this.scrollLoadEvent.loadState = false
          } else {
            this.scrollLoadEvent.loadState = true
          }
        }
      } catch (error) {
        if (isScroll) {
          this.scrollLoadEvent.loadState = true
        }
      } finally {
        this.firstLoading = false
      }
    }
  },

  computed: {
    ...mapState({
      sessionInfo: (state) => state.userInfo
    })
  },

  beforeDestroy () {
    this.scrollLoadEvent.removeScrollEvent()
  }
}
